#include "chart_widget.h"
#include "ui_chart_widget.h"
#include "dbio/db_manager.h"
#include "log/log_init.h"
ChartWidget::ChartWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChartWidget)
{
    ui->setupUi(this);

    //ui->widget->CreateHistogram();
    on_year_m_btn_clicked();
    connect(this,&ChartWidget::signOtherChart,[this](QString name,QMap<QString,int>data){
        ui->widget->createOthers(name,data);
    });
    auto list = DBManager::getInstance()->select(GET_ORDERS_DATE);
    QSet<QString> date;
    for(auto it :list){
        date.insert(it.at(0).split("-").at(0));
    }
    ui->widget->setCombox(date);
}
ChartWidget::~ChartWidget()
{
    delete ui;
}

void ChartWidget::on_year_m_btn_clicked()
{
    setCombobox(0);
    QDate currentdate = QDate::currentDate();
    QString str1 = currentdate.toString("yyyy-MM-dd");
    QString start,end;
    start  = str1.split("-").at(0)+YEAR_START;
    end = str1.split("-").at(0)+YEAR_END;
    QLOG_INFO()<<"init sql "<<QString(DEFAULT_SQL) + " where "+ QString("order_create_time BETWEEN '%1' and '%2'").arg(start).arg(end);

    QMap<QString,int> showData;
    QList<QList<QString>> data =  DBManager::getInstance()->select(QString(DEFAULT_SQL) + " where "+ QString("order_create_time BETWEEN '%1' and '%2'").arg(start).arg(end));
    for(auto it : data){
        auto time = it.at(12);
        auto list = time.split("-");
        showData[list.at(0)+"."+list.at(1)]++;//1.1
    }

    ui->widget->createOthers(str1.split("-").at(0)+"年度统计",showData,1);
    ui->widget->setNowTable();
}

void ChartWidget::on_month_btn_clicked()
{
    setCombobox(1);
    QDate currentdate = QDate::currentDate();
    QString str1 = currentdate.toString("yyyy-MM");
    QString start,end;
    start  = str1+MONTH_START;
    auto days = currentdate.daysInMonth();
    end = str1+QString(MONTH_END).arg(days);
    QLOG_INFO()<<"init sql "<<QString(DEFAULT_SQL) + " where "+ QString("order_create_time BETWEEN '%1' and '%2'").arg(start).arg(end);

    QMap<QString,int> showData;
    QList<QList<QString>> data =  DBManager::getInstance()->select(QString(DEFAULT_SQL) + " where "+ QString("order_create_time BETWEEN '%1' and '%2'").arg(start).arg(end));
    for(auto it : data){
        auto time = it.at(12);
        auto list = time.split("-");
        showData[list.at(0)+"."+list.at(1)+"."+list.at(2).mid(0,2)]++;//1.1
    }

    ui->widget->createOthers(str1+"月度统计",showData,1);
    ui->widget->setNowTable(Month);
}

void ChartWidget::on_user_y_btn_clicked()
{
    setCombobox(0);
    QMap<QString,int> showData;
    //QList<QList<QString>> data =  DBManager::getInstance()->select(SELECT_USERNAME);
    QDate currentdate = QDate::currentDate();
    QString str1 = currentdate.toString("yyyy-MM-dd");
    QString start,end;
    start  = str1.split("-").at(0)+YEAR_START;
    end = str1.split("-").at(0)+YEAR_END;

    QList<QList<QString>> data_size =  DBManager::getInstance()->select(QString(DEFAULT_SQL)/*+" WHERE cmf_user.user_nickname = '"+it.at(0)+"'"*/ +" and "+QString("cmf_orders.order_create_time BETWEEN '%1' and '%2'").arg(start).arg(end));
    //QLOG_INFO()<<"on_user_y_btn_clicked sql = "<<QString(DEFAULT_SQL)+" WHERE cmf_user.user_nickname = '"+it.at(0)+"'" +" and "+QString("cmf_orders.order_create_time BETWEEN '%1' and '%2'").arg(start).arg(end);
    for(auto it : data_size){
        showData[it.at(0)]++;
    }
    ui->widget->createOthers(str1.split("-").at(0)+"年度统计",showData,1);
    ui->widget->setNowTable(UserYser);
}

void ChartWidget::on_user_m_btn_clicked()
{
    setCombobox(1);
    QMap<QString,int> showData;
    //QList<QList<QString>> data =  DBManager::getInstance()->select(SELECT_USERNAME);
    QDate currentdate = QDate::currentDate();
    QString str1 = currentdate.toString("yyyy-MM");
    QString start,end;
    start  = str1+MONTH_START;
    auto days = currentdate.daysInMonth();
    end = str1+QString(MONTH_END).arg(days);

        QList<QList<QString>> data_size =  DBManager::getInstance()->select(QString(DEFAULT_SQL)/*+" WHERE cmf_user.user_nickname = '"+it.at(0)+"'"*/ +" and "+QString("cmf_orders.order_create_time BETWEEN '%1' and '%2'").arg(start).arg(end));
        //QLOG_INFO()<<"on_user_y_btn_clicked sql = "<<QString(DEFAULT_SQL)+" WHERE cmf_user.user_nickname = '"+it.at(0)+"'" +" and "+QString("cmf_orders.order_create_time BETWEEN '%1' and '%2'").arg(start).arg(end);
        for(auto it : data_size){
            showData[it.at(0)]++;
        }
    ui->widget->createOthers(str1+"月度统计",showData,1);
    ui->widget->setNowTable(UserMonth);
}

void ChartWidget::setCombobox(bool isMonth)
{
    auto list = DBManager::getInstance()->select(GET_ORDERS_DATE);
    QSet<QString> date;
    for(auto it :list){
        if(isMonth)
            date.insert(it.at(0).split("-").at(0)+"."+it.at(0).split("-").at(1));
        else
            date.insert(it.at(0).split("-").at(0));
    }

    ui->widget->setCombox(date);
}
